package com.zer0.tga.entity.livingentity.mob;

import com.zer0.tga.art.ArtArchive;
import com.zer0.tga.art.Bitmap;
import com.zer0.tga.entity.Entity;
import com.zer0.tga.entity.livingentity.Teemo;
import com.zer0.tga.entity.livingentity.flyingentity.EnemyBullet;
import com.zer0.tga.entity.livingentity.flyingentity.TeemoDart;
import com.zer0.tga.math.Coordinate;
import com.zer0.tga.math.Direction;
import com.zer0.tga.math.RandNum;

public class CasterMinion extends Mob {
	
	

	private int rn;
	private int actionTime = livingTime+5;
	private int idleTime=0;
	
	public void init()
	{
		//System.out.println("init");
		unitType = UnitType.ENEMY;
		health = maxHealth = 10;
		movementSpeed = 2.0;
	}

	@Override
	public void action() {
		// TODO Auto-generated method stub
	//	System.out.println(livingTime + " " + actionTime + " " + idleTime);
		if(livingTime >= actionTime)
		{
				if(livingTime == actionTime)
				{
					idleTime = RandNum.getRandNum(30, 60);
					rn = RandNum.getRandNum(1,6);
				}
			
			
			if(rn==5)
			{
				for(Entity e:level.getCurrentRoom().entities)
					if(e instanceof Teemo)
						if(e.position.x>this.position.x)
							rn=2;
						else
							rn=4;	
		
			}
			else if(rn==6)
			{
				for(Entity e:level.getCurrentRoom().entities)
					if(e instanceof Teemo)
						if(e.position.y>this.position.y)
							rn=3;
						else
							rn=1;	
			}
			
			if(rn==1)
			{
				direction = Direction.NORTH;
				move(0,-movementSpeed);
			}
			else if(rn==2)
			{
				direction = Direction.EAST;
				move(movementSpeed,0);
			}
			else if(rn==3)
			{
				direction = Direction.SOUTH;
				move(0,movementSpeed);
			}
			else if(rn==4)
			{
				direction = Direction.WEST;	
				move(-movementSpeed,0);
			}
	
			if(livingTime == actionTime + idleTime)
			{
				if(rn==1) 		level.addEntityToCurrentRoom(new EnemyBullet(position,new Coordinate(10,10),new Coordinate(position.x,position.y-1),level));
				else if(rn==2) 	level.addEntityToCurrentRoom(new EnemyBullet(position,new Coordinate(10,10),new Coordinate(position.x+1,position.y),level));
				else if(rn==3) 	level.addEntityToCurrentRoom(new EnemyBullet(position,new Coordinate(10,10),new Coordinate(position.x,position.y+1),level));
				else if(rn==4) 	level.addEntityToCurrentRoom(new EnemyBullet(position,new Coordinate(10,10),new Coordinate(position.x-1,position.y),level));
				
				
			//	level.addEntityToCurrentRoom(new Damager(new Coordinate(position.x,position.y),new Coordinate(20,20),1,400));
				actionTime += (idleTime*2);
			}
			
			
		
		}
		
		
	}
	

	/*if(rn2==1)
	{
		if(rn!=1 && rn!=3)
			move(0,-movementSpeed);
	}
	else if(rn2==2)
	{
		if(rn!=2 && rn!=4)
			move(movementSpeed,0);
	}
	else if(rn2==3)
	{
		if(rn!=1 && rn!=3)
			move(0,movementSpeed);
	}
	else if(rn2==4)
	{
		if(rn!=2 && rn!=4)
			move(-movementSpeed,0);
	}
	*/

	@Override
	public Bitmap getArt() {
		// TODO Auto-generated method stub
		return ArtArchive.badguy[0][direction.getDirection()-1];
	}
	

	public boolean collision(Entity entity, Direction dir) 
    {
		if(entity instanceof TeemoDart)
		{
			hurt(((TeemoDart) entity).damage);
			entity.removed = true;
			return true;
		}
		else if (entity instanceof Teemo)
		{
			//System.out.println("Collision!");
			((Teemo) entity).hurt(damage);
			return true;
		}
		else if(entity == null)
		{
			if(health % 2 == 0)
			{
				rn++;
				if(rn==5) 
					rn=1;
			}
			else
			{
				rn--;
				if(rn==0) 
					rn=4;
			}
			/*if(rn==1) rn=3;
			else if(rn==2) rn=4;
			else if(rn==3) rn=1;
			else if(rn==4) rn=2;
			*/
			return false;
		}

		return false;
    }


}
